/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package imp.Invoker;

import dom.Invoker;
import dom.RunMode;
import dom.command.Command;
import dom.command.Result;
import dom.command.Status;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author julian.pena
 */
public class WorkFlow implements Invoker, Command{
    
    public static final int BUFFER_SIZE = 1000;
    private Status status;
    private Result result;
    private RunMode runMode;
    private int index;
    private String name;
    private List<Command> commands;

    //<editor-fold defaultstate="collapsed" desc="constructors">
    public WorkFlow(RunMode runMode, String name) {
        index = 0;
        commands = new ArrayList<Command>();
        this.runMode = runMode;
    }
    
    public WorkFlow(RunMode runMode, String name, List<Command> commands) {
        index = 0;
        this.commands = commands;
        this.runMode = runMode;
        this.name = name;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="methods">
    @Override
    public void addCommand(Command command) {
        commands.add(command);
    }
    
    @Override
    public void executeNext() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void executeAll() {
        if (runMode == RunMode.SEQUENTIAL) {
            System.out.println("................................");
            System.out.println("sequential worFlow: " + getName());
            for (Command command : commands) {
                command.execute();
                index++;
                System.out.println("    command: " + command.getName());
            }
            System.out.println("DONE sequential worFlow: " + getName());
            System.out.println("................................");
        } else {
            System.out.println("................................");
            System.out.println("parallel worFlow: " + getName());
            Thread[] threads =  new Thread[commands.size()];
            int i = 0;
            for (Command command : commands) {
                threads[i] = new Thread(command);
                threads[i].start();
                i++;
            }
            for (Thread thread : threads) {
                try {
                    thread.join();
                } catch (InterruptedException ex) {
                }
            }
            System.out.println("DONE parallel worFlow: " + getName());
            System.out.println("................................");
        }
        
    }
    
    @Override
    public void execute() {
        executeAll();
    }

    @Override
    public void run() {
        execute();
    }
    
    @Override
    public Status getStatus() {
        return status;
    }

    @Override
    public Result getResult() {
        return result;
    }

    @Override
    public String toString() {
        return getName();
    }

    @Override
    public String getName() {
        return name;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="getters and setters">
    public List<Command> getCommands() {
        return commands;
    }
    
    public void setCommands(List<Command> commands) {
        this.commands = commands;
    }
    
    public int getIndex() {
        return index;
    }
    //</editor-fold>    
}
